﻿$(function () {
    $("#car-grid").datagrid({
        url: $.Api("Dri/Car/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#car-tools",
        remoteSort: false,
        columns:
        [[
            { field: "CarNum", title: "车牌号", width: 80, align: "center", sortable: true },
            { field: "CarType", title: "车型", width: 100, align: "center", sortable: true },
            //{
            //    field: "CarType", title: "车型", width: 100, align: "center",
            //    formatter: function (v, r, i) { return $.Api("Sys/Dictionary/GetNameByID&id=" + v); }
            //},
            { field: "BrandModel", title: "品牌型号", width: 80, align: "center", sortable: true },
            { field: "CarFrameNum", title: "车架号", width: 100, align: "center", sortable: true },
            { field: "MotorNum", title: "发动机号", width: 100, align: "center", sortable: true },
            { field: "RegisterDate", title: "注册日期", width: 80, align: "center", sortable: true },
            { field: "CertificateDate", title: "发证日期", width: 80, align: "center", sortable: true },
            { field: "AbandonDate", title: "报废日期", width: 80, align: "center", sortable: true },
            { field: "TeacherName", title: "对应教练员", width: 75, align: "center", sortable: true },
            { field: "Description", title: "描述", width: 150, align: "center", sortable: true },
            {
                field: "IsVisible", title: "启用", width: 50, align: "center", sortable: true,
                formatter: function (v, r, i) { return $.GetYesOrNo(v); }
            },
            { field: "InsertTime", title: "创建时间", width: 150, align: "center", sortable: true }
        ]],
        onDblClickRow: function (index, row) {
            Car.Update();
        }
    });
});

var Car = {
    //添加
    Insert: function () {
        Car.Mutual("添加", "dri/car/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#car-grid").datagrid("getSelected");
        if (row) {
            Car.Mutual("修改", "dri/car/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#car-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Dri/Car/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Car.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 280,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#car-model",
                    url: $.Api("Dri/Car/Save"),
                    success: function (result) { Car.Reload(); }
                });
            },
            onLoad: function () { Car.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $("#car-type").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Sys/Dictionary/List&tag=CarType"),
            valueField: 'text',
            textField: 'text'
        });

        $("#teacherID").combobox({
            width: 150, panelHeight: "auto",
            url: $.Api("Dri/Teacher/ComboBoxList"),
            valueField: 'id',
            textField: 'text'
        });
    },

    //启用
    Enable: function () {
        Car.EnableOrDisable(true);
    },
    //禁用
    Disable: function () {
        Car.EnableOrDisable(false);
    },
    //启用禁用
    EnableOrDisable: function (bool) {
        var row = $("#car-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Dri/Car/EnableOrDisable"),
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Car.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#car-grid");
    },

    //导出表格
    Export: function () {
        location.href = $.Api("Dri/Car/Export"); return false;
    }
};